package com.bkgy.controller.allprocess.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bkgy.controller.allprocess.dto.*;
import com.bkgy.controller.allprocess.entity.*;
import com.bkgy.controller.allprocess.mapper.AllProcessProductMapper;
import com.bkgy.controller.allprocess.service.AllProcessProductService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;


@Service

public class AllProcessProductServiceImpl implements AllProcessProductService {


    @Autowired
    private AllProcessProductMapper allProcessProductMapper;

    /**
     * 全流程成材率模型首页数据;
     * @param request_get AllpricessIndexReasonRequst
     * @return AllProcessIndexDetails
     * 变更：适配时间戳方式查询完成。
     */
    public  ResultData<AllProcessIndexDetails> productDetail (AllpricessIndexReasonRequst request_get) {
        //日期转换与赋值
        AllpricessIndexReasonRequst request = setdatetime(request_get);

        try
        {
            Page<?> page = new Page<>(request.getPageIndex(), request.getPageSize());

            //行数
            int count = 0;

            // 构建响应对象
            AllProcessIndexDetails indexdetails = new AllProcessIndexDetails();


            //产品品类
            List<TbAllpricessIndexEntity> results_productType = new ArrayList<>();
            if(request.getLinename()==null)
            {
                request.setLinename("cal1630");
            }
            else if(request.getLinename().equals("cal1630"))
            {
                results_productType = allProcessProductMapper.getproductTypeCal1630(page, request);
                count = (int)page.getTotal();
            }
            else if(request.getLinename().equals("cal2150"))
            {
                results_productType = allProcessProductMapper.getproductTypeCal2150(page, request);
                count = (int)page.getTotal();
            }
            else if(request.getLinename().equals("cgl1870"))
            {
                results_productType = allProcessProductMapper.getproductTypeCgl1870(page, request);
                count = (int)page.getTotal();
            }
            else if(request.getLinename().equals("tcm2280"))
            {
                results_productType = allProcessProductMapper.getproductTypeTcm2280(page, request);
                count = (int)page.getTotal();
            }
            else if(request.getLinename().equals("rcl1630"))
            {
                results_productType = allProcessProductMapper.getproductTypeRcl1630(page, request);
                count = (int)page.getTotal();
            }
            else if(request.getLinename().equals("rcl2150"))
            {
                results_productType = allProcessProductMapper.getproductTypeRcl2150(page, request);
                count = (int)page.getTotal();
            }
            else if(request.getLinename().equals("rcl1870"))
            {
                results_productType = allProcessProductMapper.getproductTypeRcl1870(page, request);
                count = (int)page.getTotal();
            }

            indexdetails.setProductType(results_productType);
            return ResultData.ResultDataSuccess(indexdetails, count);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }

    /**
     * 根据前台传入查询开始和查询结束日期，构造出yyyymmddhhmmss数据查询
     * @param request_get AllpricessIndexReasonRequst
     * @return TbAllpricessIndexEntity
     */
    public AllpricessIndexReasonRequst setdatetime(AllpricessIndexReasonRequst request_get) {
        try {
            String[] array_startTime = request_get.getDbegin().split("-");//时间格式：2025-06-01 00:00:01
            String startTime = "";//时间格式：2025/6/01 00:00:01
            String dbegin_Times = "";//时间格式：20250703075254
            if (array_startTime.length > 0) {
                String month = array_startTime[1];
                if (!month.equals("10")) {
                    month = array_startTime[1].replace("0", "");
                }

                //构建时间格式：2025/6/01 00:00:01
                startTime = array_startTime[0] + "/" + month + "/" + array_startTime[2];

                //构建时间格式：20250703075254
                String times2 = array_startTime[2].replace(" ", "");
                times2 = times2.replace(":", "");
                dbegin_Times = array_startTime[0] + array_startTime[1] + times2;
            }
            request_get.setDbegin_Rcl(startTime);//时间格式：2025/6/01 00:00:01
            request_get.setDbegin(dbegin_Times);//时间格式：20250703075254

            String[] array_endTime = request_get.getDend().split("-");//时间格式：2025-06-01 00:00:01
            String endTime = "";//时间格式：2025/6/01 00:00:01
            String endTime_Times="";//时间格式：20250703075254
            if (array_endTime.length > 0) {
                String month = array_endTime[1];
                if(!month.equals("10"))
                {
                    month= array_endTime[1].replace("0","");
                }
                //构建时间格式：2025/6/01 00:00:01
                endTime = array_endTime[0] + "/" + month + "/" + array_endTime[2];

                //构建时间格式：20250703075254
                String times2=array_endTime[2].replace(" ","");
                times2 = times2.replace(":","");
                endTime_Times=array_endTime[0]+array_endTime[1]+times2;
            }
            request_get.setDend_Rcl(endTime);
            request_get.setDend(endTime_Times);//时间格式：20250703075254

        } catch (Exception e) {
            e.printStackTrace();
        }
        return request_get;
    }
}
